﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BookExchange.Business;
using BookExchange.CustomClass.Base;
using BookExchange.ODataService;
using BookExchange.ViewModel;
using BookExchange.Configuration;

namespace BookExchange
{
    public partial class Detail : BasePage
    {
        private RequestBusiness requestBus;
        private CommentBusiness commentBus;
        private MemberBusiness memberBus;

        private int requestCode;
        private Request request;
        private Member member;
        private List<CommentViewModel> comments;
        

        public Detail()
            : base()
        {
            requestBus = new RequestBusiness();
            memberBus = new MemberBusiness();
            commentBus = new CommentBusiness();
        }

        protected void Page_Load(object sender, EventArgs e)
        {            
            //txtND.Visible = IsUserAuthenticated;
            //btnComment.Visible = IsUserAuthenticated;

            if (!IsPostBack)
            {
                LoadData();                             
                
                BindData();

                //BindComment();

            }
        }

        private void CheckRequest()
        {
            Request type = new Request();
            if (type.RequestType == RequestType.Request)
            {
                lblGiaBan.Visible = false;
                lblGiaBia.Visible = false;
            }
        }

        private void LoadData()
        {
            //abc.om/detail.aspx/c=dggs
            //Lay RequestCode tu GUI

            //Validate input
            var requestCodeString = Request.QueryString["c"];            
            if (!int.TryParse(requestCodeString, out requestCode))
            {
                return;
            }

            request = requestBus.GetRequestByCode(requestCode);

            member = memberBus.GetMemberByCode(request.MemberCode);

            //comments = commentBus.GetCommentViewModelByRequestCode(request.RequestCode);
        }

        private void BindData()
        {
            if (request == null)
            {
                //neu khong co thi tra ve empty page

                //xu ly khi khong tim thay
                return;
            }

            Image1.ImageUrl = request.BookUrl;
            lblRequestCode.Text = request.RequestCode + string.Empty;
            lblBookName.Text = request.BookName;//20 string
            lblAddress.InnerText = request.ExchangePlace;
            lblNgDangTin.InnerText = member.DisplayName;
            linkNgDangTin.NavigateUrl = string.Format("MemberInfo.aspx?c={0}", member.MemberCode);
            lblDate.InnerText = request.CreatingDate == null ? 
                string.Empty : request.CreatingDate.Value.ToString(AppConfiguration.Instance.DateFormat);
            lblSoLuong.InnerText = request.Quantity.Value.ToString(AppConfiguration.Instance.IntegerFormat);
            lblGiaBia.InnerText = request.Price1.HasValue ? request.Price1.Value.ToString(AppConfiguration.Instance.CurrencyFormat) : string.Empty;
            lblGiaBan.InnerText = request.Price2.HasValue ? request.Price2.Value.ToString(AppConfiguration.Instance.CurrencyFormat) : string.Empty;
            lblTinhTrang.InnerText = request.Status == RequestStatus.S ? RequestStatus.S : RequestStatus.F;
            txtDes.InnerText = request.Description; 
            //lblReason.InnerText = request.SellingReason; 
            lnkEditRequest.NavigateUrl = "edit.aspx?c=" + request.RequestCode;
            lnkEditRequest.Visible = IsUserAuthenticated;
        }

        //private void BindComment()
        //{

        //    rptComments.DataSource = comments;
        //    rptComments.DataBind();
        //}

        //private Comment CreateNewCommentFromGUI()
        //{
        //    //Lay noi dung tw web
        //    //string cmt = txtND.Text;
        //    if (string.IsNullOrEmpty(txtND.Text))
        //    {

        //        var newComment = CreateNewCommentFromGUI();

        //        //insert vao database
        //        commentBus.Insert(newComment);
        //        return null;
        //    }

        //    Comment newCmt = new Comment();
        //    newCmt.ID = Guid.NewGuid();
        //    newCmt.Active = 1;
        //    newCmt.CommentCode = 1;
        //    newCmt.Description = txtND.Text;
        //    newCmt.MemberCode = this.CurrentMember.MemberCode;
           
        //    //newCmt.RequestCode = requestCode;
        //    newCmt.RequestCode = 1;
        //    newCmt.CreatingDate = DateTime.Now;
        //    newCmt.LastModified = DateTime.Now;
        //    return newCmt;
        //}

        //protected void btnComment_Click(object sender, EventArgs e)
        //{
        //    if (IsUserAuthenticated)
        //    {                
        //        var newComment = CreateNewCommentFromGUI();

        //        //insert vao database
        //        commentBus.Insert(newComment);

        //        //return to this page to view the result
        //        //Response.Redirect(Request.RawUrl);
        //        RedirecToDetailPage(newComment.RequestCode);
        //        return;
                
        //    }
        //}

        
        
    }
    
}